package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.VoCCY.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class AdminRegionServiceImpl implements AdminRegionService {

    @Autowired
    MarketRegionMapper marketRegionMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketStorageMapper marketStorageMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketLogMapper marketLogMapper;


    @Override
    public CommonData query() {
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        marketRegionExample.setOrderByClause("code asc");

        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        criteria.andTypeEqualTo((byte) 1);

        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(marketRegionExample);

        LinkedList<MarketRegionVo> regionVos = new LinkedList<>();
        /*List<MarketRegionVoVo> regionVoVos = new ArrayList<>();
        List<MarketRegionVoVoVo> regionVoVoVos = new ArrayList<>();*/

        for (MarketRegion marketRegion : marketRegions) {

            List<MarketRegionVoVo> regionVoVos = new LinkedList<>();
            MarketRegionExample example = new MarketRegionExample();
            example.setOrderByClause("code asc");

            MarketRegionExample.Criteria criteria1 = example.createCriteria();
            criteria1.andTypeEqualTo((byte) 2);
            criteria1.andCodeBetween(marketRegion.getCode(), marketRegion.getCode() + 10000);

            List<MarketRegion> regions = marketRegionMapper.selectByExample(example);
            for (MarketRegion region : regions) {
                List<MarketRegionVoVoVo> regionVoVoVos = new LinkedList<>();
                MarketRegionExample example2 = new MarketRegionExample();
                example2.setOrderByClause("code asc");

                MarketRegionExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andTypeEqualTo((byte) 3);
                criteria2.andCodeBetween(region.getCode(), region.getCode() + 1000);

                List<MarketRegion> regions2 = marketRegionMapper.selectByExample(example2);


                for (MarketRegion m : regions2) {

                    MarketRegionVoVoVo regionVoVoVo = new
                            MarketRegionVoVoVo(m.getId(), m.getName(), m.getType(), m.getCode());
                    regionVoVoVos.add(regionVoVoVo);

                }
                MarketRegionVoVo marketRegionVoVo = new MarketRegionVoVo(region.getId(), region.getName(), region.getType(), region.getCode(), regionVoVoVos);
                regionVoVos.add(marketRegionVoVo);

            }
            MarketRegionVo marketRegionVo = new
                    MarketRegionVo(marketRegion.getId(), marketRegion.getName(), marketRegion.getType(), marketRegion.getCode(), regionVoVos);

            regionVos.add(marketRegionVo);


        }

        PageInfo pageInfo = new PageInfo(regionVos);

        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public CommonData brandListQuery(Integer page, Integer limit, String id, String name) {

        CommonData data = new CommonData();

        if (id == null && name == null) {
            MarketBrandExample example = new MarketBrandExample();
            example.setOrderByClause("add_time desc,update_time desc");

            long i = brandMapper.countByExample(example);
            PageHelper.startPage(page, limit);
            List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
            List<MarketBrandListVo> brandListVos = new ArrayList<>();

            for (MarketBrand marketBrand : marketBrands) {
                MarketBrandListVo marketBrandListVo = new MarketBrandListVo
                        (marketBrand.getId(), marketBrand.getName(), marketBrand.getDesc(),
                                marketBrand.getPicUrl(), marketBrand.getSortOrder(),
                                marketBrand.getFloorPrice(), marketBrand.getAddTime(),
                                marketBrand.getUpdateTime(), marketBrand.getDeleted());
                brandListVos.add(marketBrandListVo);
            }
            PageInfo pageInfo = new PageInfo(brandListVos);
            pageInfo.setTotal(i);
            CommonData data2 = CommonData.data(pageInfo);
            data = data2;
        }
        if ("".equals(id) && name == null) {
            MarketBrandExample example = new MarketBrandExample();
            example.setOrderByClause("add_time desc,update_time desc");

            long i = brandMapper.countByExample(example);
            PageHelper.startPage(page, limit);
            List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
            List<MarketBrandListVo> brandListVos = new ArrayList<>();

            for (MarketBrand marketBrand : marketBrands) {
                MarketBrandListVo marketBrandListVo = new MarketBrandListVo
                        (marketBrand.getId(), marketBrand.getName(), marketBrand.getDesc(),
                                marketBrand.getPicUrl(), marketBrand.getSortOrder(),
                                marketBrand.getFloorPrice(), marketBrand.getAddTime(),
                                marketBrand.getUpdateTime(), marketBrand.getDeleted());
                brandListVos.add(marketBrandListVo);
            }
            PageInfo pageInfo = new PageInfo(brandListVos);
            pageInfo.setTotal(i);
            CommonData data2 = CommonData.data(pageInfo);
            data = data2;
        }
        if ("".equals(id) && "".equals(name)) {
            MarketBrandExample example = new MarketBrandExample();
            example.setOrderByClause("add_time desc,update_time desc");

            long i = brandMapper.countByExample(example);
            PageHelper.startPage(page, limit);
            List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
            List<MarketBrandListVo> brandListVos = new ArrayList<>();

            for (MarketBrand marketBrand : marketBrands) {
                MarketBrandListVo marketBrandListVo = new MarketBrandListVo
                        (marketBrand.getId(), marketBrand.getName(), marketBrand.getDesc(),
                                marketBrand.getPicUrl(), marketBrand.getSortOrder(),
                                marketBrand.getFloorPrice(), marketBrand.getAddTime(),
                                marketBrand.getUpdateTime(), marketBrand.getDeleted());
                brandListVos.add(marketBrandListVo);
            }
            PageInfo pageInfo = new PageInfo(brandListVos);
            pageInfo.setTotal(i);
            CommonData data2 = CommonData.data(pageInfo);
            data = data2;
        }


        if (id != null && name == null && !"".equals(id)) {
            Integer id3 = 0;
            try {
                int id2 = Integer.parseInt(id);
                id3 = id2;
            } catch (Exception e) {
                e.printStackTrace();
                return data;
            }
            MarketBrandExample example = new MarketBrandExample();
            example.setOrderByClause("add_time desc,update_time desc");
            MarketBrandExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(id3);

            long i = brandMapper.countByExample(example);
            PageHelper.startPage(page, limit);
            List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
            List<MarketBrandListVo> brandListVos = new ArrayList<>();

            for (MarketBrand marketBrand : marketBrands) {
                MarketBrandListVo marketBrandListVo = new MarketBrandListVo
                        (marketBrand.getId(), marketBrand.getName(), marketBrand.getDesc(),
                                marketBrand.getPicUrl(), marketBrand.getSortOrder(),
                                marketBrand.getFloorPrice(), marketBrand.getAddTime(),
                                marketBrand.getUpdateTime(), marketBrand.getDeleted());
                brandListVos.add(marketBrandListVo);
            }
            PageInfo pageInfo = new PageInfo(brandListVos);
            pageInfo.setTotal(i);
            CommonData data2 = CommonData.data(pageInfo);
            data = data2;

        }

        if (id == null && name != null) {
            MarketBrandExample example = new MarketBrandExample();
            example.setOrderByClause("add_time desc,update_time desc");
            MarketBrandExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%" + name + "%");

            long i = brandMapper.countByExample(example);
            PageHelper.startPage(page, limit);
            List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
            List<MarketBrandListVo> brandListVos = new ArrayList<>();

            for (MarketBrand marketBrand : marketBrands) {
                MarketBrandListVo marketBrandListVo = new MarketBrandListVo
                        (marketBrand.getId(), marketBrand.getName(), marketBrand.getDesc(),
                                marketBrand.getPicUrl(), marketBrand.getSortOrder(),
                                marketBrand.getFloorPrice(), marketBrand.getAddTime(),
                                marketBrand.getUpdateTime(), marketBrand.getDeleted());
                brandListVos.add(marketBrandListVo);
            }
            PageInfo pageInfo = new PageInfo(brandListVos);
            pageInfo.setTotal(i);
            CommonData data2 = CommonData.data(pageInfo);
            data = data2;
        }

        return data;
    }

    @Override
    public MarketBrandCreateVo insertBrand(String name, String desc, double floorPrice, String picUrl) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String format = formatter.format(date);
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = ft.parse(format);

        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(floorPrice);
        marketBrand.setPicUrl(picUrl);
        marketBrand.setAddTime(date1);
        marketBrand.setUpdateTime(date1);

        brandMapper.insertSelective(marketBrand);

        Integer id = marketBrand.getId();
        MarketBrand market = brandMapper.selectByPrimaryKey(id);
        MarketBrandCreateVo marketBrandCreateVo = new MarketBrandCreateVo(market.getId(),
                market.getName(), market.getDesc(), market.getPicUrl(), market.getFloorPrice(),
                market.getAddTime(), market.getUpdateTime());

        return marketBrandCreateVo;
    }

    @Override
    public MarketBrandUpdateVo updateBrand(MarketBrandUpdateVo brandUpdateVo) {
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setDesc(brandUpdateVo.getDesc());
        marketBrand.setFloorPrice(brandUpdateVo.getFloorPrice());
        marketBrand.setName(brandUpdateVo.getName());
        marketBrand.setPicUrl(brandUpdateVo.getPicUrl());
        marketBrand.setUpdateTime(new Date());

        brandMapper.updateByPrimaryKeySelective(marketBrand);

        MarketBrand market = brandMapper.selectByPrimaryKey(brandUpdateVo.getId());

        MarketBrandUpdateVo marketBrandUpdateVo = new MarketBrandUpdateVo(market.getId(),
                market.getName(), market.getDesc(), market.getPicUrl(), market.getFloorPrice(),
                market.getAddTime(), market.getUpdateTime());

        return marketBrandUpdateVo;
    }

    @Override
    public void deleteBrandDelete(int id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CommonData queryCategoryList() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.setOrderByClause("id asc");
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        List<MarketCategoryListVo> categoryListVos = new LinkedList<>();

        for (MarketCategory market : marketCategories) {
            List<MarketCategoryListVoVo> categoryListVoVos = new LinkedList<>();

            MarketCategoryListVo marketCategoryListVo = new MarketCategoryListVo(market.getId(),
                    market.getName(), market.getKeywords(), market.getDesc(), market.getIconUrl(),
                    market.getPicUrl(), market.getLevel(), categoryListVoVos);

            Integer id = market.getId();
            MarketCategoryExample example2 = new MarketCategoryExample();
            example2.setOrderByClause("id asc");
            MarketCategoryExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andLevelEqualTo("L2");
            criteria2.andPidEqualTo(id);

            List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(example2);

            for (MarketCategory category : marketCategories1) {
                MarketCategoryListVoVo categoryListVoVo = new MarketCategoryListVoVo
                        (category.getId(), category.getName(), category.getKeywords(),
                                category.getDesc(), category.getIconUrl(), category.getPicUrl(),
                                category.getLevel());
                categoryListVoVos.add(categoryListVoVo);
            }

            categoryListVos.add(marketCategoryListVo);
        }

        PageInfo pageInfo = new PageInfo(categoryListVos);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public CommonData queryCategoryL1() {
        List<MarketCategoryL1Vo> categoryL1Vos = new LinkedList<>();

        MarketCategoryExample example = new MarketCategoryExample();
        example.setOrderByClause("id asc");
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");

        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);

        for (MarketCategory market : marketCategories) {
            MarketCategoryL1Vo marketCategoryL1Vo = new MarketCategoryL1Vo(market.getId(), market.getName());
            categoryL1Vos.add(marketCategoryL1Vo);
        }

        PageInfo pageInfo = new PageInfo(categoryL1Vos);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public MarketCategoryCreateVo insertCategory(MarketCategoryCreateBo createBo) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setDesc(createBo.getDesc());
        marketCategory.setIconUrl(createBo.getIconUrl());
        marketCategory.setKeywords(createBo.getKeywords());
        marketCategory.setLevel(createBo.getLevel());
        marketCategory.setName(createBo.getName());
        marketCategory.setPicUrl(createBo.getPicUrl());
        marketCategory.setPid(createBo.getPid());
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());

        marketCategoryMapper.insertSelective(marketCategory);

        Integer id = marketCategory.getId();
        MarketCategory market = marketCategoryMapper.selectByPrimaryKey(id);
        MarketCategoryCreateVo createVo = new MarketCategoryCreateVo(market.getId(),
                market.getName(), market.getKeywords(), market.getDesc(), market.getPid(),
                market.getIconUrl(), market.getPicUrl(), market.getLevel(), market.getAddTime(),
                market.getUpdateTime());

        return createVo;
    }

    @Override
    public void updateCategory(MarketCategoryListVo category) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setDesc(category.getDesc());
        marketCategory.setIconUrl(category.getIconUrl());
        marketCategory.setId(category.getId());
        marketCategory.setPicUrl(category.getPicUrl());
        marketCategory.setKeywords(category.getKeywords());
        marketCategory.setLevel(category.getLevel());
        marketCategory.setName(category.getName());
        marketCategory.setUpdateTime(new Date());

        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public void deleteCategory(Integer id) {
        marketCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CommonData queryLogList(Integer page, Integer limit, String name) {
        MarketLogExample marketLogExample = new MarketLogExample();
        marketLogExample.setOrderByClause("update_time desc");
        MarketLogExample.Criteria criteria = marketLogExample.createCriteria();
        if (name != null) {
            criteria.andAdminLike("%" + name + "%");
        }
        PageHelper.startPage(page, limit);

        List<MarketLog> marketLogs = marketLogMapper.selectByExample(marketLogExample);

        PageInfo pageInfo = new PageInfo(marketLogs);
        CommonData data = CommonData.data(pageInfo);

        return data;
    }
}
